package com.iwunu.gateway.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

public class SignUtlis {
    private static final String SIGN_FORMAT = "%s-%s-%s-%s-%s-%s-%s-%s";

    /**
     * 签名算法生成
     *
     * @param ctimestamp
     * @param cnonce
     * @param requestBody  请求体对象
     * @param requestParam
     * @param url
     * @param method
     * @param cappKey
     * @param secret
     * @return
     */
    public static String sign(String ctimestamp, String cnonce, Object requestBody, String requestParam, String uri,
            String method, String cappKey, String secret) {
        String requestBodyMd5 = "";

        if (requestBody != null) {
            String requestBodyJson = JSONObject.toJSONString(requestBody);
            if (StringUtils.isNotEmpty(requestBodyJson)) {
                requestBodyMd5 = DigestUtil.encryptMd5(requestBodyJson);
            }
        }
        String signStr = String.format(SIGN_FORMAT, uri, method, requestParam, requestBodyMd5, secret, ctimestamp,
                cnonce, cappKey);
        String sign = DigestUtil.encryptMd5(signStr);
        return sign;
    }
}
